package com.ldcc.report_system.ui.user;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.ldcc.report_system.ApplicationConfig;
import com.ldcc.report_system.ReportApplication;
import com.ldcc.report_system.api.results.PerPersonnel;
import com.ldcc.report_system.api.results.User;
import com.ldcc.report_system.component.UserInfoItemLayout;
import com.ldcc.report_system.databinding.FragmentUserInfoBinding;
import com.ldcc.report_system.util.SharedPreferencesUtils;
import com.ldcc.report_system.util.http.ClientRequestManage;
import com.ldcc.report_system.util.http.HttpObserver;
import com.ldcc.report_system.util.http.ServerResponse;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author wwj[yeshengkele@outlook.com]
 * @description 个人中心界面
 */
public class UserInfoFragment extends Fragment {
    public static final String TAG = "UserInfoFragment";
    protected FragmentUserInfoBinding userInfoBinding;
    private AppCompatActivity parentActivity;

    public UserInfoFragment(AppCompatActivity activity) {
        this.parentActivity = activity;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.userInfoBinding = FragmentUserInfoBinding.inflate(inflater, container, false);
        return this.userInfoBinding.getRoot();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 初始化
        this.init();
    }

    private void init() {

    }


    @Override
    public void onResume() {
        //显示时所作的事情
        super.onResume();
        this.loadUserInfo();
        this.loadPerPersonnel();
    }

    /**
     * @description 加载用户信息
     */
    private void loadUserInfo() {
        ClientRequestManage.getInstance().getUserApi().userInfo().subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<User>(ReportApplication.getContext(), null) {
                    @Override
                    public void onSuccess(ServerResponse<User> response_entity) {
                        User user = response_entity.getData();
                        // 设置用户名
                        UserInfoItemLayout.setUserInfoItemValueText(userInfoBinding.userInfoName, user.getName());
                        // 设置性别
                        UserInfoItemLayout.setUserInfoItemValueText(userInfoBinding.userInfoGender, user.getGender() == 0 ? "男" : "女");
                        // 所在部门
                        UserInfoItemLayout.setUserInfoItemValueText(userInfoBinding.deptName, user.getDeptName());
                        // 现任职务
                        UserInfoItemLayout.setUserInfoItemValueText(userInfoBinding.duty, user.getDuty());
                        // 电话
                        UserInfoItemLayout.setUserInfoItemValueText(userInfoBinding.mobile, user.getMobile());
                    }
                });
    }

    /**
     * @description 加载用户人事资料
     */
    private void loadPerPersonnel() {
        String userId = (String) SharedPreferencesUtils.getParam(ReportApplication.getContext(), ApplicationConfig.USER_ID_KEY, "");
        ClientRequestManage.getInstance().getUserApi().getPerPersonnelById(userId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<PerPersonnel>(ReportApplication.getContext(), null) {
                    @Override
                    public void onSuccess(ServerResponse<PerPersonnel> response_entity) {
                        PerPersonnel perPersonnel = response_entity.getData();
                        if (perPersonnel != null) {
                            // 民族
                            UserInfoItemLayout.setUserInfoItemValueText(userInfoBinding.userInfoFamous, perPersonnel.getEthnic());
                            // 证件号码
                            UserInfoItemLayout.setUserInfoItemValueText(userInfoBinding.identityCard, perPersonnel.getIdentityCard());
                            // 学历
                            UserInfoItemLayout.setUserInfoItemValueText(userInfoBinding.education, perPersonnel.getEducation());
                            // 学位
                            UserInfoItemLayout.setUserInfoItemValueText(userInfoBinding.degree, perPersonnel.getDegree());
                            // 政治面貌
                            UserInfoItemLayout.setUserInfoItemValueText(userInfoBinding.politicsStatus, perPersonnel.getPoliticsStatus());
                            // 毕业院校
                            UserInfoItemLayout.setUserInfoItemValueText(userInfoBinding.graduationSchool, perPersonnel.getGraduationSchool());
                            // 所学专业
                            UserInfoItemLayout.setUserInfoItemValueText(userInfoBinding.major, perPersonnel.getMajor());
                            // 毕业证书编号
                            UserInfoItemLayout.setUserInfoItemValueText(userInfoBinding.graduationNumber, perPersonnel.getGraduationNumber());
                        }
                    }
                });
    }


}